package com.sztest.jayjun.ui;

import android.view.View;

import com.sztest.jayjun.R;
import com.sztest.jayjun.adapter.DebugRecycleViewAdapter;
import com.sztest.jayjun.base.BaseActivity;
import com.sztest.jayjun.bean.DebugBean;
import com.sztest.jayjun.bean.DebugItemBean;
import com.sztest.jayjun.constant.Constant;
import com.sztest.jayjun.databinding.ActivityDebugBinding;
import com.sztest.jayjun.mmkv.MMKVUtils;
import com.sztest.jayjun.utils.LogUtils;
import com.sztest.jayjun.utils.ToastUtils;

import java.util.ArrayList;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

/*
 *  @项目名：  jayjun
 *  @包名：    com.sztest.jayjun.ui
 *  @文件名:   DebugActivity
 *  @创建者:   okmin
 *  @创建时间:  2022/6/28 11:07
 *  @描述：    调试界面
 */
public class DebugActivity extends BaseActivity {

    ActivityDebugBinding mDebugBinding;
    private DebugRecycleViewAdapter mAdapter1Energy;
    private DebugRecycleViewAdapter mAdapter2suspend;
    private DebugRecycleViewAdapter mAdapter3frequency;
    DebugBean mDebugBean;
    private int modeType;
    @Override
    protected void setDataBinding() {
        mDebugBinding= DataBindingUtil.setContentView(this,R.layout.activity_debug);
    }

    int debugCount=0;

    @Override
    protected void initView() {
        modeType= getIntent().getIntExtra(Constant.modeType,1);
        defaultData();
        setPush();
        setspeed();
        mDebugBinding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mDebugBinding.tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.e("save");
                saveData();
                setResult(2244);
                finish();
            }
        });
        mDebugBinding.tvReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.e("reset");
                defaultData();
                debugCount++;
                if (debugCount==8){
                    ToastUtils.show("开启日志保存文件！");
                    LogUtils.sLog2FileSwitch=true;
                }
            }
        });
    }

    public void defaultData(){
        mDebugBean=new DebugBean();
        mDebugBean.modeType=modeType;
        mDebugBean= MMKVUtils.getInstance().getDebugBean(mDebugBean);
        setPush(mDebugBean.push);
        refreSpeed();
        setRecycle1();
        setRecycle2();
        setRecycle3();
    }


    private void setspeed() {

        mDebugBinding.ivSlowAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDebugBean.speed_slow=mDebugBean.speed_slow +0.5;
                refreSpeed();
            }
        });

        mDebugBinding.ivSlowDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDebugBean.speed_slow==0) {
                    return;
                }
                mDebugBean.speed_slow=mDebugBean.speed_slow -0.5;
                refreSpeed();
            }
        });

        mDebugBinding.ivFastAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDebugBean.speed_fast=mDebugBean.speed_fast  +0.5;
                refreSpeed();
            }
        });

        mDebugBinding.ivFastDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDebugBean.speed_fast==0) {
                    return;
                }
                mDebugBean.speed_fast=mDebugBean.speed_fast -0.5;
                refreSpeed();
            }
        });


        mDebugBinding.ivNorAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDebugBean.speed_nor=mDebugBean.speed_nor +0.5;
                refreSpeed();
            }
        });
        mDebugBinding.ivNorDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDebugBean.speed_nor==0) {
                    return;
                }
                mDebugBean.speed_nor=mDebugBean.speed_nor -0.5;
                refreSpeed();
            }
        });

    }

    private void refreSpeed() {
        mDebugBinding.tvSpeedFast.setText(mDebugBean.speed_fast+"");
        mDebugBinding.tvSpeedNor.setText(mDebugBean.speed_nor+"");
        mDebugBinding.tvSpeedSlow.setText(mDebugBean.speed_slow+"");
    }

    private void setPush() {
        mDebugBinding.tvPush1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPush(1);
            }
        });

        mDebugBinding.tvPush2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPush(2);
            }
        });

        mDebugBinding.tvPush3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPush(3);
            }
        });
        mDebugBinding.tvPush4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPush(4);
            }
        });
    }

    void setPush(int push){
        mDebugBean.push=push;
        if (push==1) {
            mDebugBinding.tvPush1.setSelected(true);
            mDebugBinding.tvPush2.setSelected(false);
            mDebugBinding.tvPush3.setSelected(false);
            mDebugBinding.tvPush4.setSelected(false);
        }else if (push==2) {
            mDebugBinding.tvPush1.setSelected(false);
            mDebugBinding.tvPush2.setSelected(true);
            mDebugBinding.tvPush3.setSelected(false);
            mDebugBinding.tvPush4.setSelected(false);
        }else if (push==3) {
            mDebugBinding.tvPush1.setSelected(false);
            mDebugBinding.tvPush2.setSelected(false);
            mDebugBinding.tvPush3.setSelected(true);
            mDebugBinding.tvPush4.setSelected(false);
        }else if (push==4) {
            mDebugBinding.tvPush1.setSelected(false);
            mDebugBinding.tvPush2.setSelected(false);
            mDebugBinding.tvPush3.setSelected(false);
            mDebugBinding.tvPush4.setSelected(true);
        }
    }

    @Override
    protected void initData() {

    }

    void setRecycle1(){
        mDebugBinding.recyclerveiw1energy.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter1Energy = new DebugRecycleViewAdapter(1);
        ArrayList<DebugItemBean> list= new ArrayList<>();
        for (int i = 1; i < 11; i++) {
            DebugItemBean debugItemBean = new DebugItemBean();
            debugItemBean.setType(1);
            debugItemBean.setPosition(i+"");
            debugItemBean.setNum(60.0d+i*2);
            debugItemBean.setModeType(modeType);
            debugItemBean=MMKVUtils.getInstance().getDebugItemBean(debugItemBean);
            list.add(debugItemBean);
        }
        mAdapter1Energy.setData(list);
        mDebugBinding.recyclerveiw1energy.setAdapter(mAdapter1Energy);

        mAdapter1Energy.setAIListI(new DebugRecycleViewAdapter.AIListI() {
            @Override
            public void onAddClick(int position) {
                DebugItemBean debugItemBean = mAdapter1Energy.getData().get(position);
                debugItemBean.setNum(debugItemBean.getNum()+1);
                mAdapter1Energy.notifyDataSetChanged();
            }

            @Override
            public void onDownClick(int position) {
                DebugItemBean debugItemBean = mAdapter1Energy.getData().get(position);
                if (debugItemBean.getNum()==0) {
                    return;
                }
                debugItemBean.setNum(debugItemBean.getNum()-1);
                mAdapter1Energy.notifyDataSetChanged();
            }
        });
    }

    public static double fyTime(int i){
        return 1.3d;
    }

    void setRecycle2(){
        mDebugBinding.recyclerveiw2suspend.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter2suspend = new DebugRecycleViewAdapter(2);
        ArrayList<DebugItemBean> list= new ArrayList<>();

        for (int i = 1; i < 11; i++) {
            DebugItemBean debugItemBean = new DebugItemBean();
            debugItemBean.setType(2);
            debugItemBean.setPosition(i+"");
            debugItemBean.setNum(fyTime(i));
            debugItemBean.setModeType(modeType);
            debugItemBean=MMKVUtils.getInstance().getDebugItemBean(debugItemBean);
            list.add(debugItemBean);
        }


        /*DebugItemBean debugItemBean = new DebugItemBean();
        debugItemBean.setType(2);
        debugItemBean.setPosition("1");
        debugItemBean.setNum(2.8d);
        debugItemBean.setModeType(modeType);
        debugItemBean=MMKVUtils.getInstance().getDebugItemBean(debugItemBean);
        list.add(debugItemBean);


        DebugItemBean debugItemBean2 = new DebugItemBean();
        debugItemBean2.setType(2);
        debugItemBean2.setPosition("2");
        debugItemBean2.setNum(3.4d);
        debugItemBean2.setModeType(modeType);
        debugItemBean2=MMKVUtils.getInstance().getDebugItemBean(debugItemBean2);
        list.add(debugItemBean2);

        DebugItemBean debugItemBean3 = new DebugItemBean();
        debugItemBean3.setType(2);
        debugItemBean3.setPosition("3");
        debugItemBean3.setNum(4.0d);
        debugItemBean3.setModeType(modeType);
        debugItemBean3=MMKVUtils.getInstance().getDebugItemBean(debugItemBean3);
        list.add(debugItemBean3);


        DebugItemBean debugItemBean4 = new DebugItemBean();
        debugItemBean4.setType(2);
        debugItemBean4.setPosition("4");
        debugItemBean4.setNum(4.6d);
        debugItemBean4.setModeType(modeType);
        debugItemBean4=MMKVUtils.getInstance().getDebugItemBean(debugItemBean4);
        list.add(debugItemBean4);


        DebugItemBean debugItemBean5 = new DebugItemBean();
        debugItemBean5.setType(2);
        debugItemBean5.setPosition("5");
        debugItemBean5.setNum(5.2d);
        debugItemBean5.setModeType(modeType);
        debugItemBean5=MMKVUtils.getInstance().getDebugItemBean(debugItemBean5);
        list.add(debugItemBean5);*/



        mAdapter2suspend.setData(list);
        mDebugBinding.recyclerveiw2suspend.setAdapter(mAdapter2suspend);

        mAdapter2suspend.setAIListI(new DebugRecycleViewAdapter.AIListI() {
            @Override
            public void onAddClick(int position) {
                DebugItemBean debugItemBean = mAdapter2suspend.getData().get(position);
                debugItemBean.setNum(debugItemBean.getNum()+0.1);
                mAdapter2suspend.notifyDataSetChanged();
            }

            @Override
            public void onDownClick(int position) {
                DebugItemBean debugItemBean = mAdapter2suspend.getData().get(position);
                if (debugItemBean.getNum()<0.1) {
                    return;
                }
                debugItemBean.setNum(debugItemBean.getNum()-0.1);
                mAdapter2suspend.notifyDataSetChanged();
            }
        });
    }

    void setRecycle3(){
        mDebugBinding.recyclerveiw3frequency.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter3frequency = new DebugRecycleViewAdapter(3);
        ArrayList<DebugItemBean> list= new ArrayList<>();

        DebugItemBean debugItemBean = new DebugItemBean();
        debugItemBean.setType(3);
        debugItemBean.setPosition("1");
        debugItemBean.setNum(4d);
        debugItemBean.setModeType(modeType);
        debugItemBean=MMKVUtils.getInstance().getDebugItemBean(debugItemBean);
        list.add(debugItemBean);

        DebugItemBean debugItemBean2 = new DebugItemBean();
        debugItemBean2.setType(3);
        debugItemBean2.setPosition("2");
        debugItemBean2.setNum(5d);
        debugItemBean2.setModeType(modeType);
        debugItemBean2=MMKVUtils.getInstance().getDebugItemBean(debugItemBean2);
        list.add(debugItemBean2);

        DebugItemBean debugItemBean3 = new DebugItemBean();
        debugItemBean3.setType(3);
        debugItemBean3.setPosition("3");
        debugItemBean3.setNum(6d);
        debugItemBean3.setModeType(modeType);
        debugItemBean3=MMKVUtils.getInstance().getDebugItemBean(debugItemBean3);
        list.add(debugItemBean3);


        DebugItemBean debugItemBean4 = new DebugItemBean();
        debugItemBean4.setType(3);
        debugItemBean4.setPosition("4");
        debugItemBean4.setNum(8d);
        debugItemBean4.setModeType(modeType);
        debugItemBean4=MMKVUtils.getInstance().getDebugItemBean(debugItemBean4);
        list.add(debugItemBean4);


        DebugItemBean debugItemBean5 = new DebugItemBean();
        debugItemBean5.setType(3);
        debugItemBean5.setPosition("5");
        debugItemBean5.setNum(10.6d);
        debugItemBean5.setModeType(modeType);
        debugItemBean5=MMKVUtils.getInstance().getDebugItemBean(debugItemBean5);
        list.add(debugItemBean5);



        mAdapter3frequency.setData(list);
        mDebugBinding.recyclerveiw3frequency.setAdapter(mAdapter3frequency);

        mAdapter3frequency.setAIListI(new DebugRecycleViewAdapter.AIListI() {
            @Override
            public void onAddClick(int position) {
                DebugItemBean debugItemBean = mAdapter3frequency.getData().get(position);
                debugItemBean.setNum(debugItemBean.getNum()+0.1);
                mAdapter3frequency.notifyDataSetChanged();
            }

            @Override
            public void onDownClick(int position) {
                DebugItemBean debugItemBean = mAdapter3frequency.getData().get(position);
                if (debugItemBean.getNum()<0.1) {
                    return;
                }
                debugItemBean.setNum(debugItemBean.getNum()-0.1);
                mAdapter3frequency.notifyDataSetChanged();
            }
        });
    }

    public void saveData(){
        MMKVUtils.getInstance().setDebugBean(mDebugBean);
        for (DebugItemBean bean : mAdapter1Energy.getData()) {
            MMKVUtils.getInstance().setDebugItemBean(bean);
        }
        for (DebugItemBean bean : mAdapter2suspend.getData()) {
            MMKVUtils.getInstance().setDebugItemBean(bean);
        }
        for (DebugItemBean bean : mAdapter3frequency.getData()) {
            MMKVUtils.getInstance().setDebugItemBean(bean);
        }
    }

}
